<?php

require_once 'include/global.inc.php';

if (isset($_POST['submit']))
{
	// test the incoming data
	$handicap = (int) $_POST['handicap'];

	// set the players color if random
	if ('random' == $_POST['color'])
	{
		$color = (1 == mt_rand(0, 1)) ? 'black' : 'white';
		$handicap = 0; // there should be no handicap if the color is random
	}
	else
	{
		$color = $_POST['color'];
	}

	// set the player ids based on color
	switch ($_POST['color'])
	{
		case 'black' :
			$black_id = $_SESSION['player_id'];
			$white_id = sani($_POST['opponent_id']);
			break;

		case 'white' : // no break
		default :
			$white_id = $_SESSION['player_id'];
			$black_id = sani($_POST['opponent_id']);
			break;
	}

	// make sure the board size is odd
	$board_size = (floor($_POST['board_size'] / 2) * 2) + 1;

	// and between 9 and 19
	if (19 < $board_size)
	{
		$board_size = 19;
	}
	else if (9 > $board_size)
	{
		$board_size = 9;
	}

	// make sure the handicap is valid
	if (1 == $handicap)
	{
		$handicap = 2;
	}

	// set the turn based on handicap
	$turn = (0 == $handicap) ? 'Black' : 'White';

	// create the new game
	$query = "
		INSERT
		INTO ".T_GAME."
		SET g_id = NULL
			, g_black_player_id = '{$black_id}'
			, g_white_player_id = '{$white_id}'
			, g_handicap = '{$handicap}'
			, g_board_size = '{$board_size}'
			, g_moves = ''
			, g_num_moves = 0
			, g_status = '{$turn}'
			, g_last_move = 0
			, g_create_date = NOW( )
	";
	$mysql->query($query, __LINE__, __FILE__);

	header('Location: index.php');
	exit;
}

// get the player data for the player drop-down list
$query = "
	SELECT p_id
		, p_username
	FROM ".T_PLAYER."
	WHERE p_id != '{$_SESSION['player_id']}'
	ORDER BY p_username
";
$players = $mysql->fetch_array($query, __LINE__, __FILE__);

$player_option_html = '<select name="opponent_id">';

foreach ($players as $player)
{
	$player_option_html .= "<option value=\"{$player['p_id']}\">{$player['p_username']}</option>\n";
}

$player_option_html .= '</select>';

// start the page
echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
	"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<title>iShudan : Create Game</title>

	<meta http-equiv="Content-Language" content="en-us" />
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<meta http-equiv="Content-Style-Type" content="text/css" />

	<link rel="stylesheet" href="css/iShudan.css" type="text/css" />

</head>
<body>
<div id="links"><?php echo $CFG_SITELINKS; ?></div>
<h1>iShudan :: create game</h1>
	<form method="post" action="" style="margin:10em;">
	<a href="index.php">home</a>
	opponent <?php echo $player_option_html; ?> &nbsp; | &nbsp;
	your color <select name="color">
		<option>random</option>
		<option>black</option>
		<option>white</option>
	</select> &nbsp; | &nbsp;
	board size <select name="board_size">
		<option>9</option>
		<option>11</option>
		<option>13</option>
		<option>15</option>
		<option>17</option>
		<option>19</option>
	</select> &nbsp; | &nbsp;
	handicap (black) <select name="handicap">
		<option>0</option>
		<option>2</option>
		<option>3</option>
		<option>4</option>
		<option>5</option>
		<option>6</option>
		<option>7</option>
		<option>8</option>
		<option>9</option>
	</select> &nbsp; | &nbsp;
	<input type="submit" name="submit" value="submit" />
</form>
<?php call('GLOBALS'); ?>
</body>
</html>